package com.best.onegous;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.Adapter.HomeAdapter;
import com.best.onegous.Refresh.XListView;
import com.best.onegous.Refresh.XListView.IXListViewListener;
import com.best.onegous.Rewrite.SlideShowView;
import com.best.onegous.View.AutoTextView;
import com.best.onegous.agreement.ApiInterface;
import com.best.onegous.agreement.Apps;
import com.best.onegous.agreement.Common;
import com.best.onegous.details.Searchone;
import com.best.onegous.http.BusinessResponse;
import com.best.onegous.http.HttpClients;
import com.best.onegous.viewutils.MyProgressDialog;
import com.best.onegous.viewutils.RedDialog;
import com.loopj.android.http.RequestParams;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.ViewGroup;

public class Home extends Fragment implements IXListViewListener,
		OnScrollListener, OnClickListener, BusinessResponse {

	private XListView mListView;
	private List<JSONObject> mDatas = new ArrayList<JSONObject>();
	private HomeAdapter mAdapter;
	private View bannerView, bannerView1, fenleiview;
	private SlideShowView slideshowView;
	private LinearLayout invis, fenlei;
	private ImageView one, two, three, four, top_one, top_two, top_three,
			top_four;
	private TextView one1, two1, three1, four1, top_one1, top_two1, top_three1,
			top_four1;
	private Intent intent;
	private HttpClients httpclient;
	private LayoutInflater inflaters;
	private String order = "1";
	private AutoTextView mTextView0;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_home, container, false);
		inflaters = inflater;
		mListView = (XListView) view.findViewById(R.id.listview);
		invis = (LinearLayout) view.findViewById(R.id.invis);
		top_one = (ImageView) view.findViewById(R.id.top_one);
		top_two = (ImageView) view.findViewById(R.id.top_two);
		top_three = (ImageView) view.findViewById(R.id.top_three);
		top_four = (ImageView) view.findViewById(R.id.top_four);
		top_one1 = (TextView) view.findViewById(R.id.top_one1);
		top_two1 = (TextView) view.findViewById(R.id.top_two1);
		top_three1 = (TextView) view.findViewById(R.id.top_three1);
		top_four1 = (TextView) view.findViewById(R.id.top_four1);
		// one.setOnClickListener(new ToolsItemListener(0));
		top_one1.setOnClickListener(new ToolsItemListener(0));
		// two.setOnClickListener(new ToolsItemListener(1));
		top_two1.setOnClickListener(new ToolsItemListener(1));
		// three.setOnClickListener(new ToolsItemListener(2));
		top_three1.setOnClickListener(new ToolsItemListener(2));
		// four.setOnClickListener(new ToolsItemListener(3));
		top_four1.setOnClickListener(new ToolsItemListener(3));
		view.findViewById(R.id.search).setOnClickListener(this);

		bannerView = (View) inflaters.inflate(R.layout.activity_home_top, null);
		mTextView0 = (AutoTextView) bannerView.findViewById(R.id.switcher0);
		mTextView0.next("135****189", "5", "�˴λ��ƻ��apple��iphone 6s plus");
		slideshowView = (SlideShowView) bannerView
				.findViewById(R.id.slideshowView);
		fenlei = (LinearLayout) bannerView.findViewById(R.id.fenlei);
		mListView.addHeaderView(bannerView);

		bannerView1 = (View) inflaters.inflate(R.layout.title_top, null);
		one = (ImageView) bannerView1.findViewById(R.id.one);
		two = (ImageView) bannerView1.findViewById(R.id.two);
		three = (ImageView) bannerView1.findViewById(R.id.three);
		four = (ImageView) bannerView1.findViewById(R.id.four);
		one1 = (TextView) bannerView1.findViewById(R.id.one1);
		two1 = (TextView) bannerView1.findViewById(R.id.two1);
		three1 = (TextView) bannerView1.findViewById(R.id.three1);
		four1 = (TextView) bannerView1.findViewById(R.id.four1);
		// one.setOnClickListener(new ToolsItemListener(0));
		one1.setOnClickListener(new ToolsItemListener(0));
		// two.setOnClickListener(new ToolsItemListener(1));
		two1.setOnClickListener(new ToolsItemListener(1));
		// three.setOnClickListener(new ToolsItemListener(2));
		three1.setOnClickListener(new ToolsItemListener(2));
		// four.setOnClickListener(new ToolsItemListener(3));
		four1.setOnClickListener(new ToolsItemListener(3));

		mListView.addHeaderView(bannerView1);

	
		mAdapter = new HomeAdapter(getActivity(), mDatas);
		mListView.setAdapter(mAdapter);
		mListView.setPullRefreshEnable(true);
		mListView.setPullLoadEnable(false);
		mListView.setXListViewListener(this);
		mListView.setOnScrollListener(this);

		httpclient = new HttpClients(getActivity());
		httpclient.addResponseListener(this);

		Load();
		oneLoad();
		return view;
	}

	private class ToolsItemListener implements View.OnClickListener {
		private int id;

		public ToolsItemListener(int id) {
			this.id = id;
		}

		@Override
		public void onClick(View v) {
			clearTitle();
			switch (id) {
			case 0:
				one.setImageResource(R.drawable.hot);
				one1.setTextColor(getResources().getColor(R.color.them));

				top_one.setImageResource(R.drawable.hot);
				top_one1.setTextColor(getResources().getColor(R.color.them));
				order = "1";
				Load();
				// one1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
				break;
			case 1:
				two.setImageResource(R.drawable.jindu2);
				two1.setTextColor(getResources().getColor(R.color.them));

				top_two.setImageResource(R.drawable.jindu2);
				top_two1.setTextColor(getResources().getColor(R.color.them));
				order = "2";
				Load();
				// two1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
				break;
			case 2:
				three.setImageResource(R.drawable.xinpin2);
				three1.setTextColor(getResources().getColor(R.color.them));

				top_three.setImageResource(R.drawable.xinpin2);
				top_three1.setTextColor(getResources().getColor(R.color.them));
				order = "3";
				Load();
				// three1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
				break;
			case 3:
				four.setImageResource(R.drawable.jiage1);
				four1.setTextColor(getResources().getColor(R.color.them));

				top_four.setImageResource(R.drawable.jiage1);
				top_four1.setTextColor(getResources().getColor(R.color.them));
				order = "4";
				Load();
				// four1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
				break;
			default:
				mAdapter.notifyDataSetChanged();
				break;
			}
		}
	}

	private void clearTitle() {
		one.setImageResource(R.drawable.hot12);
		two.setImageResource(R.drawable.jindu1);
		three.setImageResource(R.drawable.xinpin1);
		four.setImageResource(R.drawable.jiage);
		one1.setTextColor(getResources().getColor(R.color.zi));
		two1.setTextColor(getResources().getColor(R.color.zi));
		three1.setTextColor(getResources().getColor(R.color.zi));
		four1.setTextColor(getResources().getColor(R.color.zi));

		top_one.setImageResource(R.drawable.hot12);
		top_two.setImageResource(R.drawable.jindu1);
		top_three.setImageResource(R.drawable.xinpin1);
		top_four.setImageResource(R.drawable.jiage);
		top_one1.setTextColor(getResources().getColor(R.color.zi));
		top_two1.setTextColor(getResources().getColor(R.color.zi));
		top_three1.setTextColor(getResources().getColor(R.color.zi));
		top_four1.setTextColor(getResources().getColor(R.color.zi));
		/*
		 * one1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
		 * two1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
		 * three1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
		 * four1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
		 */
	}

	private void oneLoad() {
		httpclient.httpclient(ApiInterface.SHOUYELUNBO, null, false);
		httpclient.httpclient(ApiInterface.SHANGPINFENLEI, null, false);
	}

	private void Load() {
		
		  RequestParams params = new RequestParams();
		  params.add("order",order);
		httpclient.httpclient(ApiInterface.SHOUYESHANGPIN, params, true);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				mListView.stopLoadMore();
			}
		}, 2000);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		oneLoad();
		Load();
		/*
		 * final RedDialog mDialog = new RedDialog(getActivity());
		 * mDialog.show(); Handler handler = new Handler();
		 * handler.postDelayed(new Runnable() {
		 * 
		 * @Override public void run() { mListView.stopRefresh(); } }, 2000);
		 */
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub

		if (firstVisibleItem >= 2) {
			invis.setVisibility(View.VISIBLE);
		} else {
			invis.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.search:

			intent = new Intent(getActivity(), Searchone.class);
			getActivity().startActivity(intent);
			break;

		default:
			break;
		}
	}

	private TextView fenleitext;
	private ImageView fenleiimg;

	@Override
	public void OnMessageResponse(String url, JSONObject jo)
			throws JSONException {
		// TODO Auto-generated method stub
		if (url.equals(ApiInterface.SHOUYESHANGPIN)) {
			mListView.stopRefresh();
		}
		
		if (null != jo) {
			if (url.equals(ApiInterface.SHOUYELUNBO)) {

				slideshowView.gundongimg(jo.getJSONArray("Rows"));

			}
			if (url.equals(ApiInterface.SHANGPINFENLEI)) {
				JSONArray Rows = jo.getJSONArray("Rows");
				for (int i = 0, s = Rows.length(); i < s; i++) {
					JSONObject item = Rows.getJSONObject(i);
					fenleiview = inflaters.inflate(R.layout.biaoqian_item, null);
					fenleitext = (TextView) fenleiview
							.findViewById(R.id.fenleitext);
					fenleiimg = (ImageView) fenleiview
							.findViewById(R.id.fenleiimg);
					fenleiimg.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub

						}
					});
					Apps.imageLoader.displayImage(
							Common.PHOTOURL + item.getString("img"), fenleiimg,
							Apps.touxiang);
					fenleitext.setText(item.getString("name"));
					fenlei.addView(fenleiview);
				}

			}
			
			if (url.equals(ApiInterface.SHOUYESHANGPIN)) {
				JSONArray data = jo.getJSONArray("data");
				mDatas.clear();
				for (int i = 0, s = data.length(); i < s; i++) {
					mDatas.add(data.getJSONObject(i));
				}
				mAdapter.notifyDataSetChanged();
			}
			
		}
	}

}
